﻿using B.Auth.APi.Application.Command.Menus;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Menuss
{
    public class AddMenuCommandHand : IRequestHandler<AddMenuCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<MenuModel> _Menu;

        public AddMenuCommandHand(IBaseRepository<MenuModel> menu)
        {
            _Menu = menu;
        }

        public Task<ApiResult<int>> Handle(AddMenuCommand request, CancellationToken cancellationToken)
        {

            var result = new ApiResult<int>();
            var menu = new MenuModel();
            menu.MenuName=request.MenuName;
            menu.Url=request.Url;
            menu.PID=request.PID;
            menu.Sort = 0;
            menu.MenuDel = 0;
         
            menu.CreateTime = DateTime.Now;
            menu.UpdateTime = null;
            menu.CreateUserName = "张三";
            menu.UodateUserName = "";


         var rest= _Menu.GetAll().Any(x => x.PID == request.PID && x.MenuName == request.MenuName && x.MenuDel == 0);

            if (rest)
            {

                result.Message = "同一菜单下不允许名称重复";
                result.Code = ApiEnum.fail;
                return Task.FromResult(result);
            }
            _Menu.Add(menu);

          
            result.Message = "菜单添加成功";
            result.Code = ApiEnum.ok;

            return Task.FromResult(result); 

        }
    }
}
